package web;

import helperclasses.Bill;
import helperclasses.MyDataColector;
import helperclasses.Product;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.ModifyBill;
import model.ProductsName;

/**
 * Servlet implementation class ViewBills
 */
@WebServlet("/ViewBills")
public class ViewBills extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ViewBills() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		if (request.getParameter("m").equals("add")) {

			try {
				// Get object that contains all the product names for
				// autocomplete fields
				ProductsName pn = new ProductsName();
				request.setAttribute("productsNames", pn.getDesfasurat());
				MyDataColector dc = new MyDataColector();
				request.setAttribute("numarNir", dc.getNumarNir() + 1);
				request.setAttribute("suppliersNames", dc.getFurnizorNames());
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			RequestDispatcher view = request.getRequestDispatcher("adaugarefactura.jsp");
			view.forward(request, response);

		} else if (request.getParameter("m").equals("view")) {
				MyDataColector dc = new MyDataColector();
				try {
					request.setAttribute("listafacturi", dc.getBillsList());
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
												
			RequestDispatcher view = request.getRequestDispatcher("vizualizarefactura.jsp");
			view.forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Bill myBill = new Bill();
		String alterResult = "";
		Product myProduct;
		int count = 1;
		ModifyBill modifyBill;
		
		myBill.setCompany(request.getParameter("numecompanie"));
		myBill.setRestaurant(request.getParameter("restaurant1"));
		myBill.setGestionar(request.getParameter("gestionar"));
		myBill.setDataFactura(request.getParameter("datafactura"));
		myBill.setNirNumber(Integer.parseInt(request.getParameter("numarnir")));
		myBill.setFurnizor(request.getParameter("supName"));
		myBill.setCodFactura(request.getParameter("codfactura"));

		while (request.getParameter(count + "_1") != null) {
			myProduct = new Product();
			myProduct.setProductName(request.getParameter(count + "_1"));
			myProduct.setCantitate(Integer.parseInt(request.getParameter(count + "_2")));
			myProduct.setPretIntrare(Double.parseDouble(request.getParameter(count + "_3")));
			myBill.getProducts().add(myProduct);
			count++;
		}

		modifyBill = new ModifyBill(myBill);
		try {
			
			if (modifyBill.SaveBill()) {
				alterResult = "Salvarea realizata cu succes";
			} else {
				alterResult = "Salvarea a intampinat o problema.";
			}
		} catch (SQLException e) {	
			e.printStackTrace();
		}

		request.setAttribute("alterResult", alterResult);
		RequestDispatcher view = request.getRequestDispatcher("adaugarefactura.jsp");
		view.forward(request, response);
	}

}
